/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.listener.PropertyViewListener;
import com.cea.papyrus.diagram.sequence.sedi.edit.AbstractLinkEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.SeDiSubpartEditPart;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;

/**
 * Basic Papyrus implementation for property view section. Each section in papyrus should
 * override this section, not {@link AbstractPropertySection}.
 */
public class PropertyViewSection extends com.cea.papyrus.core.tabbedproperties.PropertyViewSection {

    /**
     * Tells the section that the selection has changed.
     */
    public void selectionChanged(ISelection selection) {
        Assert.isTrue(selection instanceof IStructuredSelection);
        // remove listeners on old selected element ()
        if (_umlListener != null && umlElement != null) {
            (umlElement).eAdapters().remove(_umlListener);
            _umlListener = null;
        }

        // add listener on the new element
        Object input = ((IStructuredSelection) getSelection()).getFirstElement();

        if (input instanceof IUMLElementEditPart) {
            graphicalElement = (IUMLElementEditPart<?>) input;
            umlElement = ((IUMLElementEditPart<?>) input).getUmlElement();
            _umlListener = new PropertyViewListener(this);
            (umlElement).eAdapters().add(_umlListener);
            
            if ((input instanceof GraphNodeEditPart) || (input instanceof GraphEdgeEditPart)) {
                diagramElement = (DiagramElement) ((AbstractGraphicalEditPart) input).getModel();
            } else if (input instanceof SeDiSubpartEditPart
                    || input instanceof AbstractLinkEditPart) {
                diagramElement = (DiagramElement) ((EditPart) input).getModel();
            }
        }

    }
}
